package hotel.quartz.oversea.job.task;

import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.constant.CommonUrlConstants;
import hotel.base.oversea.constant.MeituanConstants;
import hotel.base.oversea.constant.enums.MessageEnums;
import hotel.base.oversea.constant.quartz.UrlSwitchsEnums;
import hotel.base.oversea.utils.DateUtil;
import hotel.base.oversea.utils.GsonUtils;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.utils.SendUtils;
import hotel.quartz.oversea.config.service.RedisService;
import hotel.quartz.oversea.util.JobRestTemplateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;


@Slf4j
@Component
public class SwitchsPopGoodsBreakfastTask {
    private String timestamp = "mcohgdt"+String.valueOf(System.currentTimeMillis());
    @Autowired
    private RedisService redisService;
    @Autowired
    private RestTemplate restTemplate;
    private void setTimestamp(String timestamp){
        this.timestamp = timestamp;
    }

    //@Async 标识这个方法异步执行，可返回void或Future
    @Async
    public void executeRequestBreakfast() {
        log.info("MeituanSaveApiLogTask {} >>>>>>>>> 异步线程 消费早餐队列  开始执行时间：{}",timestamp, DateUtil.getNowTime());
        String host = CommonUrlConstants.SWITCHS_HOST;
        String url = host+ UrlSwitchsEnums.addDowngGoods.BREAKFAST.getValue();
        log.info("requestBreakfast Switchs Pop Goods Breakfast Task {} 携程消费队列处理静态产品早餐失效新增定时器 进入方法 requestBreakfast",timestamp);

        long size = redisService.queueSize(MeituanConstants.REDIS_QUEUE_HOTEL_BREAKFAST_JSON_KEY);
        Integer successCount =0;
        if(size>0){
            SendUtils.dingMsgNotLog("[执行] pop处理产品‘早餐’队列 ", DateUtil.getCurrTime()+" 处理："+size+" 预计耗时："+size*7/60+" 分钟", MessageEnums.genre.GOODS_STATUS_AND_PRICE.getCode(),restTemplate);
            try {
                Thread.sleep(5000);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
            for (int i = 0; i < size; i++) {
                String oneHotelJOSN = (String)redisService.RPop(MeituanConstants.REDIS_QUEUE_HOTEL_BREAKFAST_JSON_KEY);
                if(!ObjectUtils.isEmpty(oneHotelJOSN)){
                    //发起请求
                    log.info("requestBreakfast Switchs Pop Goods Breakfast Task 调用早餐变更接口前 队列："+redisService.queueSize(MeituanConstants.REDIS_QUEUE_HOTEL_BREAKFAST_JSON_KEY));
                    Result response = JobRestTemplateUtils.post(url,oneHotelJOSN,restTemplate);
                    long size2 = redisService.queueSize(MeituanConstants.REDIS_QUEUE_HOTEL_BREAKFAST_JSON_KEY);
                    log.info("requestBreakfast Switchs Pop Goods Breakfast Task {} 携程消费队列处理静态产品早餐失效新增定时器  requestBreakfast 调用携程接口结束，早餐队列size={}",timestamp,size2);
                    if(response.getCode()!= CommonConstants.SUCCESS){
                        log.error("requestBreakfast Switchs Pop Goods Breakfast Task 异常  早餐  当前酒店推送早餐携程失败：rs={}", GsonUtils.GsonString(response));
                        SendUtils.dingMsg("'早餐'变化！推送携程失败 ", DateUtil.getCurrTime()+" 推送内容："+oneHotelJOSN+" 接口返回异常:"+ GsonUtils.GsonString(response), MessageEnums.genre.GOODS_STATUS_AND_PRICE.getCode(),restTemplate);
                    }else{
                        successCount+=1;
                        log.info("requestBreakfast Switchs Pop Goods Breakfast Task '早餐'变化 推送携程成功:"+response);
                        //SendUtils.dingMsg("查询到'失效'产品,推送成功 ",DateUtil.getCurrTime()+" 内容："+oneHotelJOSN, MessageEnums.genre.GOODS_STATUS_AND_PRICE.getCode(),restTemplate);
                    }
                    if(null != redisService.get("todoSwitchsPopQueueAboutAddDownJob_breakfast_close")){
                        log.info("requestBreakfast Switchs Pop Goods Breakfast Task    主动停止‘早餐’推送  ");
                        break;
                    }
                    Integer time = 7000;
                    if(null != redisService.get("requestBreakfast_time")){
                        time = (Integer)redisService.get("requestBreakfast_time");
                    }
                    try {
                        Thread.sleep(time);
                    } catch (Exception e) {
                        log.error(e.getMessage(), e);
                    }
                }
            }
            if(BigDecimal.valueOf(successCount).compareTo(BigDecimal.valueOf(size))!=0){
                SendUtils.dingMsgNotLog("[结束] 处理队列'早餐'产品 ", DateUtil.getCurrTime()+" 已处理数据："+successCount+" 数据数量处理'异常'实际应处理："+size, MessageEnums.genre.GOODS_STATUS_AND_PRICE.getCode(),restTemplate);
            }else{
                SendUtils.dingMsgNotLog("[结束] 处理队列'早餐'产品 ", DateUtil.getCurrTime()+" 处理完成数据："+successCount, MessageEnums.genre.GOODS_STATUS_AND_PRICE.getCode(),restTemplate);
            }
        }
        log.info("requestBreakfast Switchs Pop Goods Breakfast Task 结束 早餐 产品 推携程");

    }


}
